from sympy import Rational,Integer,sympify,symbols
import sympy as sym
import fractions
from decimal import Decimal


def isSymType(symObj, type):
    # 将输入的 type 字符串转换成 SymPy 可识别的表达式对象
    type_expr = str(sympify(type))
    flag = isinstance(symObj,Rational)
    flag2 = type_expr == 'rational'
    # 如果 symObj 本身就是 type 类型，返回 True
    if (isinstance(symObj,Integer) or isinstance(symObj,int)) and type_expr == 'integer' :
        return True
    elif isinstance(symObj,Rational) and type_expr == 'rational':
        return True
    elif isinstance(symObj,Decimal) and type_expr == 'vpareal':
        return True
    elif isinstance(symObj,complex) and type_expr == 'complex':
        return True
    elif ((isinstance(symObj,Integer) or isinstance(symObj,int))
          or isinstance(symObj,Rational) 
          or isinstance(symObj,Decimal)
          or isinstance(symObj,fractions.Fraction)) and (type_expr == 'real' or type_expr == 'number'):
        return True
    
    # 对于其他情况，返回 False
    else:
        return False

def hasSymType(expr, symType):
    # Returns true if the expression expr contains a symbol of the specified type
    if not isinstance(expr, sym.Expr):
        return False
    symList = expr.args
    for symbol in symList:
        if isSymType(symbol, symType):
            return True
    return False

x,y,z = sym.symbols('x y z')
expr = x**2 + sym.sin(y) + sym.exp(z+3)  + 4
assert hasSymType(expr, 'integer') == True
assert hasSymType(expr, 'float') == False

expr =  3 * sym.pi + sym.exp(x)
assert hasSymType(expr, 'rational') == False
assert hasSymType(expr, 'float') == False